package lxbAdmin.userSystem.server.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.netty.util.internal.StringUtil;
import lxbAdmin.userSystem.common.constants.SystemUserConstant;
import lxbAdmin.userSystem.dao.UserMapper;
import lxbAdmin.userSystem.dto.User;
import lxbAdmin.userSystem.dto.UserDTO;
import lxbAdmin.userSystem.entity.SystemUser;
import lxbAdmin.userSystem.entity.SystemUserRole;
import lxbAdmin.userSystem.server.SystemUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SystemUserServiceImpl implements SystemUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDTO getSystemUser(User user) {
        if (StringUtil.isNullOrEmpty(user.getSort())) {
            user.setSort(SystemUserConstant.USER_NAME);
        }
        if (StringUtil.isNullOrEmpty(user.getSortType())) {
            user.setSortType(SystemUserConstant.SORT_TYPE);
        }
        UserDTO result = new UserDTO();
        result.setPageNo(user.getPageNo());
        result.setPageNum(user.getPageNum());
        PageHelper.startPage(user.getPageNo(), user.getPageNum());
        List<SystemUser> systemUserList = userMapper.getSystemUser(user);
        result.setTotal(((Page<?>) systemUserList).getTotal());
        result.setTableData(systemUserList);
        return result;
    }

    @Override
    public SystemUser selectByUserId(String userId) {
        return userMapper.selectByUserId(userId);
    }

    @Override
    public List<SystemUser> selectAllUser(User user) {
        return userMapper.selectAllUser(user);
    }

    @Override
    public Integer addSystemUser(User user) {
        userMapper.addSystemUser(user);
        int userId = userMapper.selectMaxUserId();
        userMapper.addSystemUserRole(userId,user.getRole());
        return userId;
    }

    @Override
    public void editSystemUser(User user) {
        userMapper.editSystemUser(user);
        userMapper.editSystemUserRole(user);
    }

    @Override
    public void delSystemUser(String userId) {
        userMapper.delSystemUser(userId);
        userMapper.delSystemUserRole(userId);
    }

    @Override
    public List<SystemUserRole> selectRole() {
        return userMapper.selectRole();
    }
}
